#include "key.h"
#include "button.h"
#include "controller.h"
#include "panel.h"

static void btn_process_combine(uint8_t *btnState);
static void btn_process_power_ac(void);
static void btn_process_mode_ac(void);
static void btn_process_speed_ac(void);
static void btn_process_up_ac(void);
static void btn_process_down_ac(void);
static void btn_process_page_ac(void);
static void btn_process_power_dn(void);
static void btn_process_mode_dn(void);
static void btn_process_speed_dn(void);
static void btn_process_up_dn(void);
static void btn_process_down_dn(void);
static void btn_process_page_dn(void);
static void btn_process_power_fresh(void);
static void btn_process_mode_fresh(void);
static void btn_process_speed_fresh(void);
static void btn_process_up_fresh(void);
static void btn_process_down_fresh(void);
static void btn_process_page_fresh(void);
static void btn_process_power_engineer(void);
static void btn_process_mode_engineer(void);
static void btn_process_speed_engineer(void);
static void btn_process_up_engineer(void);
static void btn_process_down_engineer(void);
static void btn_process_page_engineer(void);
static void btn_process_power_advance(void);
static void btn_process_mode_advance(void);
static void btn_process_speed_advance(void);
static void btn_process_up_advance(void);
static void btn_process_down_advance(void);
static void btn_process_page_advance(void);

/* public function prototype ------------------------------------------------ */


void button_process(void)
{
    uint8_t btnState[BUTTON_NUM];
    for(uint8_t i = 0; i < BUTTON_NUM; i++)
    {
        btnState[i] = button_get_state(i);
        if(btnState[i] != Release)
        {
            if(get_low_power_state() != 0)
            {
                btnState[i] = Release;  
                set_mode_blink(1);
                system_wake_up();
                break;
            }
            else
            {
                set_mode_blink(0);
                system_wake_up();
                for(uint8_t i = 0; i < 3; i++)
                {
                    panel_tubex_blink_set(i, 0);
                    panel_unitx_blink_set(i, 0);
                }
            }
        }
    }
    if(get_lock_state() != 0)
    {
        if(btnState[Btn_Power] != LongPressed)
        {
            for(uint8_t i = 0; i < BUTTON_NUM; i++)
            {
                /* 清除按键动作 */
                btnState[i] = Release;
            }
        }
    }
    switch(get_page())
    {
        case PAGE_SYSTEM:
        {
            /* 按键锁 */
            if(btnState[Btn_Power] == LongPressed)
            {
                set_lock(!get_lock_state());
            }
            switch(get_system())
            {
                case SYSTEM_AC:
                {
                    /* 空调开机状态 */
                    if(get_power_ac() != 0)
                    {
                        /* 关机 */
                        if(btnState[Btn_Power] == Pressed)
                        {
                            btn_process_power_ac();
                        }
                        /* 切换空调模式 */
                        if(btnState[Btn_Mode] == Pressed)
                        {
                            btn_process_mode_ac();
                        }
                        /* 切换风速 */
                        if(btnState[Btn_Speed] == Pressed)
                        {
                            btn_process_speed_ac();
                        }
                        /* 上调温度 */
                        if(btnState[Btn_Up] == Pressed)
                        {
                            btn_process_up_ac();
                        }
                        /* 下调温度 */
                        if(btnState[Btn_Down] == Pressed)
                        {
                            btn_process_down_ac();
                        }
                        /* 切换页面 */
                        if(btnState[Btn_Page] == Pressed)
                        {
                            btn_process_page_ac();
                        }
                    }
                    /* 空调关机状态 */
                    else
                    {
                        /* 开机 */
                        if(btnState[Btn_Power] == Pressed)
                        {
                            btn_process_power_ac();
                        }
                        /* 切换页面 */
                        if(btnState[Btn_Page] == Pressed)
                        {
                            btn_process_page_ac();
                        }
                        /* 组合键 */
                        btn_process_combine(btnState);
                    }
                }
                break;
                case SYSTEM_DN:
                {
                    /* 地暖开机状态 */
                    if(get_power_dn() != 0)
                    {
                        /* 关机 */
                        if(btnState[Btn_Power] == Pressed)
                        {
                            btn_process_power_dn();
                        }
                        /* 切换空调模式 */
                        if(btnState[Btn_Mode] == Pressed)
                        {
                            btn_process_mode_dn();
                        }
                        /* 切换风速 */
                        if(btnState[Btn_Speed] == Pressed)
                        {
                            btn_process_speed_dn();
                        }
                        /* 上调温度 */
                        if(btnState[Btn_Up] == Pressed)
                        {
                            btn_process_up_dn();
                        }
                        /* 下调温度 */
                        if(btnState[Btn_Down] == Pressed)
                        {
                            btn_process_down_dn();
                        }
                        /* 切换页面 */
                        if(btnState[Btn_Page] == Pressed)
                        {
                            btn_process_page_dn();
                        }
                    }
                    /* 地暖关机状态 */
                    else
                    {
                        /* 开机 */
                        if(btnState[Btn_Power] == Pressed)
                        {
                            btn_process_power_dn();
                        }
                        /* 切换页面 */
                        if(btnState[Btn_Page] == Pressed)
                        {
                            btn_process_page_dn();
                        }
                        /* 组合键 */
                        btn_process_combine(btnState);
                    }
                }
                break;
                case SYSTEM_FRESH:
                {
                    /* 新风开机状态 */
                    if(get_power_fresh() != 0)
                    {
                        /* 关机 */
                        if(btnState[Btn_Power] == Pressed)
                        {
                            btn_process_power_fresh();
                        }
                        /* 切换空调模式 */
                        if(btnState[Btn_Mode] == Pressed)
                        {
                            btn_process_mode_fresh();
                        }
                        /* 切换风速 */
                        if(btnState[Btn_Speed] == Pressed)
                        {
                            btn_process_speed_fresh();
                        }
                        /* 上调温度 */
                        if(btnState[Btn_Up] == Pressed)
                        {
                            btn_process_up_fresh();
                        }
                        /* 下调温度 */
                        if(btnState[Btn_Down] == Pressed)
                        {
                            btn_process_down_fresh();
                        }
                        /* 切换页面 */
                        if(btnState[Btn_Page] == Pressed)
                        {
                            btn_process_page_fresh();
                        }
                    }
                    /* 新风关机状态 */
                    else
                    {
                        /* 开机 */
                        if(btnState[Btn_Power] == Pressed)
                        {
                            btn_process_power_fresh();
                        }
                        /* 切换页面 */
                        if(btnState[Btn_Page] == Pressed)
                        {
                            btn_process_page_fresh();
                        }
                        /* 组合键 */
                        btn_process_combine(btnState);
                    }
                }
                break;
            }
        }
        break;
        case PAGE_ENGINEER:
        {
            if(btnState[Btn_Power] == Pressed)
            {
                btn_process_power_engineer();
            }
            if(btnState[Btn_Mode] == Pressed)
            {
                btn_process_mode_engineer();
            }
            if(btnState[Btn_Speed] == Pressed)
            {
                btn_process_speed_engineer();
            }
            if(btnState[Btn_Up] == Pressed)
            {
                btn_process_up_engineer();
            }
            if(btnState[Btn_Down] == Pressed)
            {
                btn_process_down_engineer();
            }
            if(btnState[Btn_Page] == Pressed)
            {
                btn_process_page_engineer();
            }
        }
        break;
        case PAGE_ADVANCE:
        {
            if(btnState[Btn_Power] == Pressed)
            {
                btn_process_power_advance();
            }
            if(btnState[Btn_Mode] == Pressed)
            {
                btn_process_mode_advance();
            }
            if(btnState[Btn_Speed] == Pressed)
            {
                btn_process_speed_advance();
            }
            if(btnState[Btn_Up] == Pressed)
            {
                btn_process_up_advance();
            }
            if(btnState[Btn_Down] == Pressed)
            {
                btn_process_down_advance();
            }
            if(btnState[Btn_Page] == Pressed)
            {
                btn_process_page_advance();
            }
        }
        break;
        case PAGE_ERROR:
        {
            
        }
        break;
    }
}


/* 按键处理函数 ************************************/
/* 组合键处理函数 */
static void btn_process_combine(uint8_t *btnState)
{
    if(btnState[Btn_Mode] == LongPressed)
    {
        /* 退网 */
        if(btnState[Btn_Up] == LongPressed)
        {
            set_net(!get_net_state());
        }
        /* 恢复出厂设置 */
        else if(btnState[Btn_Down] == LongPressed)
        {
            sys_params_deinit();
        }
        /* 工程师模式 */
        else
        {
            set_page(PAGE_ENGINEER);
        }
    }

    if(btnState[Btn_Up] == LongPressed)
    {
        /* 高级模式 */
        if(btnState[Btn_Down] == LongPressed)
        {
            set_page(PAGE_ADVANCE);
        }
    }
}
/* 空调模式下POWER按键处理函数 */
static void btn_process_power_ac(void)
{
    set_power_ac(!get_power_ac());
}
/* 空调模式下MODE按键处理函数 */
static void btn_process_mode_ac(void)
{
    reset_controller_ac();
    uint8_t acModeSelect = get_ac_mode_select();
    uint8_t acMode = get_ac_mode();
    switch(acMode)
    {
        case ACMODE_AIR:
        {
            if(acModeSelect & BIT_ACMODE_DRY)
            {
                acMode = ACMODE_DRY;
            }
            else if(acModeSelect & BIT_ACMODE_COOL)
            {
                acMode = ACMODE_COOL;
            }
            else if(acModeSelect & BIT_ACMODE_HEAT)
            {
                acMode = ACMODE_HEAT;
            }
        }
        break;
        case ACMODE_COOL:
        {   
            if(acModeSelect & BIT_ACMODE_HEAT)
            {
                acMode = ACMODE_HEAT;
            }
            else if(acModeSelect & BIT_ACMODE_AIR)
            {
                acMode = ACMODE_AIR;
            }
            else if(acModeSelect & BIT_ACMODE_DRY)
            {
                acMode = ACMODE_DRY;
            }
        }
        break;
        case ACMODE_HEAT:
        {   
            if(acModeSelect & BIT_ACMODE_AIR)
            {
                acMode = ACMODE_AIR;
            }
            else if(acModeSelect & BIT_ACMODE_DRY)
            {
                acMode = ACMODE_DRY;
            }
            else if(acModeSelect & BIT_ACMODE_COOL)
            {
                acMode = ACMODE_COOL;
            }
        }
        break;
        case ACMODE_DRY:
        {
            if(acModeSelect & BIT_ACMODE_COOL)
            {
                acMode = ACMODE_COOL;
            }
            else if(acModeSelect & BIT_ACMODE_HEAT)
            {
                acMode = ACMODE_HEAT;
            }
            else if(acModeSelect & BIT_ACMODE_AIR)
            {
                acMode = ACMODE_AIR;
            }
        }   
        break;
    }
    set_ac_mode(acMode);
}
/* 空调模式下SPEED按键处理函数 */
static void btn_process_speed_ac(void)
{                     
    reset_controller_ac();

    uint8_t speedSet = get_speedSet_ac();
    uint8_t acMode = get_ac_mode();
    switch(acMode)
    {
        case ACMODE_AIR:
        {
            /* 切换风速 */
            if(speedSet == SPEED_SET_AUTO)
            {
                speedSet = SPEED_SET_LOW;
                
            }
            else if(speedSet < SPEED_SET_HIGH)
            {
                speedSet++;
            }
            else
            {
                speedSet = SPEED_SET_LOW;
            }
        }
        break;
        case ACMODE_COOL:
        {   
            if(speedSet < SPEED_SET_HIGH)
            {
                speedSet++;
            }
            else
            {
                speedSet = SPEED_SET_AUTO;
            }
        }
        break;
        case ACMODE_HEAT:
        {   
            if(speedSet < SPEED_SET_HIGH)
            {
                speedSet++;
            }
            else
            {
                speedSet = SPEED_SET_AUTO;
            }
        }
        break;
        case ACMODE_DRY:
        {
            speedSet = SPEED_SET_LOW;
        }   
        break;
    }
    set_speedSet_ac(speedSet);
}
/* 空调模式下UP按键处理函数 */
static void btn_process_up_ac(void)
{
    /* 复位状态机 */
    reset_controller_ac();
    /* 空调设定温度闪烁 */
    panel_tubex_blink_set(1, 7);
    uint8_t tempSet = get_tempSet_ac();
    uint8_t tempLimitUp = get_temp_upLimit();
    uint8_t ecoState = get_eco_state();
    uint8_t acMode = get_ac_mode();

    /* 经济模式 */
    if((ecoState != 0) && (acMode == ACMODE_HEAT))
    {
        if(tempSet < ECO_TEMP_SET_LIMIT_UP)
        {
            tempSet++;
        }
    }
    else
    {
        if(tempSet < tempLimitUp)
        {
            tempSet++;
        }
    }
    set_tempSet_ac(tempSet);
}
/* 空调模式下DOWN按键处理函数 */
static void btn_process_down_ac(void)
{
    /* 复位状态机 */
    reset_controller_ac();
    /* 空调设定温度闪烁 */
    panel_tubex_blink_set(1, 7);

    uint8_t tempSet = get_tempSet_ac();
    uint8_t tempLimitDown = get_temp_downLimit();
    uint8_t ecoState = get_eco_state();
    uint8_t acMode = get_ac_mode();

    /* 经济模式 */
    if((ecoState != 0) && (acMode == ACMODE_COOL))
    {
        if(tempSet > ECO_TEMP_SET_LIMIT_DOWN)
        {
            tempSet--;
        }
    }
    else
    {
        if(tempSet > tempLimitDown)
        {
            tempSet--;
        }
    }
    set_tempSet_ac(tempSet);
}
/* 空调模式下PAGE按键处理函数 */
static void btn_process_page_ac(void)
{
    switch(get_machine_enable())
    {
        case SYSTEM_AC_DN:
        {
            set_system(SYSTEM_DN);
        }
        break;
        case SYSTEM_AC_FRESH:
        {
            set_system(SYSTEM_FRESH);
        }
        break;
        case SYSTEM_FULL:
        {
            set_system(SYSTEM_DN);
        }
        break;
    }
}
/* 地暖模式下POWER按键处理函数 */
static void btn_process_power_dn(void)
{
    set_power_dn(!get_power_dn());
}
/* 地暖模式下MODE按键处理函数 */
static void btn_process_mode_dn(void)
{
    /* 地暖机型无模式选项 */
}
/* 地暖模式下SPEED按键处理函数 */
static void btn_process_speed_dn(void)
{
    /* 地暖机型无风速选项 */
}
/* 地暖模式下UP按键处理函数 */
static void btn_process_up_dn(void)
{
    /* 复位状态机 */
    reset_controller_dn();
    /* 地暖设定温度闪烁 */
    panel_tubex_blink_set(0, 7);
    uint8_t tempSet = get_tempSet_dn();
    uint8_t tempLimitUp = get_temp_upLimit();
    uint8_t ecoState = get_eco_state();

    /* 经济模式 */
    if(ecoState != 0)
    {
        if(tempSet < ECO_TEMP_SET_LIMIT_UP)
        {
            tempSet++;
        }
    }
    else
    {
        if(tempSet < tempLimitUp)
        {
            tempSet++;
        }
    }
    set_tempSet_dn(tempSet);
}
/* 地暖模式下DOWN按键处理函数 */
static void btn_process_down_dn(void)
{
    /* 复位状态机 */
    reset_controller_dn();
    /* 空调设定温度闪烁 */
    panel_tubex_blink_set(0, 7);

    uint8_t tempSet = get_tempSet_dn();
    uint8_t tempLimitDown = get_temp_downLimit();

    if(tempSet > tempLimitDown)
    {
        tempSet--;
    }
    set_tempSet_dn(tempSet);
}
/* 地暖模式下PAGE按键处理函数 */
static void btn_process_page_dn(void)
{
    switch(get_machine_enable())
    {
        case SYSTEM_AC_DN:
        {
            set_system(SYSTEM_AC);
        }
        break;
        case SYSTEM_DN_FRESH:
        {
            set_system(SYSTEM_FRESH);
        }
        break;
        case SYSTEM_FULL:
        {
            set_system(SYSTEM_FRESH);
        }
        break;
    }
}
/* 新风模式下POWER按键处理函数 */
static void btn_process_power_fresh(void)
{
    set_power_fresh(!get_power_fresh());
}
/* 新风模式下MODE按键处理函数 */
static void btn_process_mode_fresh(void)
{
    /* 新风机型无模式选项 */
}
/* 新风模式下SPEED按键处理函数 */
static void btn_process_speed_fresh(void)
{
    uint8_t speedSet = get_speedSet_fresh();

    if(speedSet < SPEED_SET_HIGH)
    {
        speedSet++;
    }
    else
    {
        speedSet = SPEED_SET_LOW;
    }
    set_speedSet_fresh(speedSet);
}
/* 新风模式下UP按键处理函数 */
static void btn_process_up_fresh(void)
{
    /* 新风模式无设定温度 */
}
/* 新风模式下DOWN按键处理函数 */
static void btn_process_down_fresh(void)
{
    /* 新风模式无设定温度 */
}
/* 新风模式下PAGE按键处理函数 */
static void btn_process_page_fresh(void)
{
    switch(get_machine_enable())
    {
        case SYSTEM_AC_FRESH:
        {
            set_system(SYSTEM_AC);
        }
        break;
        case SYSTEM_DN_FRESH:
        {
            set_system(SYSTEM_DN);
        }
        break;
        case SYSTEM_FULL:
        {
            set_system(SYSTEM_AC);
        }
        break;
    }
}
/* 工程师模式下POWER按键处理函数 */
static void btn_process_power_engineer(void)
{
    panel_tubex_blink_set(2, 0);
    panel_unitx_blink_set(2, 0);
    set_select_state(0);
    set_page(PAGE_SYSTEM);
    set_menu(MENU_CALIB);
}
/* 工程师模式下MODE按键处理函数 */
static void btn_process_mode_engineer(void)
{
    uint8_t temp = !get_selected_state();
                
    set_select_state(temp);

    if(temp != 0)
    {
        if(get_menu() != MENU_UNIT)
        {
            panel_tubex_blink_set(2, 0xFFFF);
        }
        else
        {
            panel_unitx_blink_set(2, 0xFFFF);
        }
    }
    else
    {
        panel_tubex_blink_set(2, 0);
        panel_unitx_blink_set(2, 0);
    }
}
/* 工程师模式下SPEED按键处理函数 */
static void btn_process_speed_engineer(void)
{
    /* 工程师模式下SPEED无操作 */
}
/* 工程师模式下UP按键处理函数 */
static void btn_process_up_engineer(void)
{
    uint8_t temp = get_selected_state();
    signed char data;
    if(temp != 0)
    {
        panel_tubex_blink_set(2, 0xFFFF);
        panel_unitx_blink_set(2, 0xFFFF);
        switch(get_menu())
        {
            case MENU_CALIB:
            {
                data = get_calib_signed();
                if(data < CALIB_TEMP_LIMIT_UP)
                {
                    data++;
                    set_calib_signed(data);
                }
            }
            break;
            case MENU_ECO:
            {
                set_eco(!get_eco_state());
            }
            break;
            case MENU_SPEED_CTRL:
            {
                set_speed_ctrl(!get_speed_ctrl_state());
            }
            break;
            case MENU_LOW_TEMP_PROTECT:
            {
                set_low_temp_protect(!get_low_temp_protect_state());
            }
            break;
            case MENU_MEMORY:
            {
                set_memory_mode(!get_memory_mode_state());
            }
            break;
            case MENU_BACKLASH_UP:
            {
                data = get_backlash_up();
                if(data < BACKLASH_UP_LIMIT_UP)
                {
                    data++;
                    set_backlash_up(data);
                }
            }
            break;
            case MENU_BACKLASH_DOWN:
            {
                data = get_backlash_down_signed();
                if(data < BACKLASH_DOWN_LIMIT_UP)
                {
                    if(data < (get_backlash_up() - 1))
                    {
                        data++;
                        set_backlash_down_signed(data);
                    }
                }
            }
            break;
            case MENU_TEMP_SET_LIMIT_UP:
            {
                temp = get_temp_upLimit();
                if(temp < TEMP_SET_LIMIT_UP)
                {
                    temp++;
                    set_temp_upLimit(temp);
                }
            }
            break;
            case MENU_TEMP_SET_LIMIT_DOWN:
            {
                temp = get_temp_downLimit();
                if(temp < get_temp_upLimit())
                {
                    temp++;
                    set_temp_downLimit(temp);
                }
            }
            break;
            case MENU_COOL_ENABLE:
            {
                set_ac_cool_enable(!get_ac_cool_enable());
            }
            break;
            case MENU_HEAT_ENABLE:
            {
                set_ac_heat_enable(!get_ac_heat_enable());
            }
            break;
            case MENU_AIR_ENABLE:
            {
                set_ac_air_enable(!get_ac_air_enable());
            }
            break;
            case MENU_DRY_ENABLE:
            {
                set_ac_dry_enable(!get_ac_dry_enable());
            }
            break;
            case MENU_UNIT:
            {
                set_unit(!get_unit());
                unit_convert();
            }
            break;
            case MENU_ADDRESS:
            {
                temp = get_device_addr();
                if(temp < 0xFF)
                {
                    temp++;
                    set_device_addr(temp);
                }
            }
            break;
        }
    }
    else
    {
        temp = get_menu();
        if(temp < MENU_ADDRESS)
        {
            temp++;
        }
        else
        {
            temp = MENU_CALIB;
        }
        set_menu(temp);
    }
}
/* 工程师模式下DOWN按键处理函数 */
static void btn_process_down_engineer(void)
{
    uint8_t temp = get_selected_state();
    signed char data;
    if(temp != 0)
    {
        panel_tubex_blink_set(2, 0xFFFF);
        panel_unitx_blink_set(2, 0xFFFF);
        switch(get_menu())
        {
            case MENU_CALIB:
            {
                data = get_calib_signed();
                if(data > CALIB_TEMP_LIMIT_DOWN)
                {
                    data--;
                    set_calib_signed(data);
                }
            }
            break;
            case MENU_ECO:
            {
                set_eco(!get_eco_state());
            }
            break;
            case MENU_SPEED_CTRL:
            {
                set_speed_ctrl(!get_speed_ctrl_state());
            }
            break;
            case MENU_LOW_TEMP_PROTECT:
            {
                set_low_temp_protect(!get_low_temp_protect_state());
            }
            break;
            case MENU_MEMORY:
            {
                set_memory_mode(!get_memory_mode_state());
            }
            break;
            case MENU_BACKLASH_UP:
            {
                data = get_backlash_up();
                if(data > BACKLASH_UP_LIMIT_DOWN)
                {
                    if(data > (get_backlash_down_signed() + 1))
                    {
                        data--;
                        set_backlash_up(data);
                    }
                }
            }
            break;
            case MENU_BACKLASH_DOWN:
            {
                data = get_backlash_down_signed();
                if(data > BACKLASH_DOWN_LIMIT_DOWN)
                {
                    data--;
                    set_backlash_down_signed(data);
                }
            }
            break;
            case MENU_TEMP_SET_LIMIT_UP:
            {
                temp = get_temp_upLimit();
                if(temp > get_temp_downLimit())
                {
                    temp--;
                    set_temp_upLimit(temp);
                }
            }
            break;
            case MENU_TEMP_SET_LIMIT_DOWN:
            {
                temp = get_temp_downLimit();
                if(temp > TEMP_SET_LIMIT_DOWN)
                {
                    temp--;
                    set_temp_downLimit(temp);
                }
            }
            break;
            case MENU_COOL_ENABLE:
            {
                set_ac_cool_enable(!get_ac_cool_enable());
            }
            break;
            case MENU_HEAT_ENABLE:
            {
                set_ac_heat_enable(!get_ac_heat_enable());
            }
            break;
            case MENU_AIR_ENABLE:
            {
                set_ac_air_enable(!get_ac_air_enable());
            }
            break;
            case MENU_DRY_ENABLE:
            {
                set_ac_dry_enable(!get_ac_dry_enable());
            }
            break;
            case MENU_UNIT:
            {
                set_unit(!get_unit());
                unit_convert();
            }
            break;
            case MENU_ADDRESS:
            {
                temp = get_device_addr();
                if(temp > 0x01)
                {
                    temp--;
                    set_device_addr(temp);
                }
            }
            break;
        }
    }
    else
    {
        temp = get_menu();
        if(temp > MENU_CALIB)
        {
            temp--;
        }
        else
        {
            temp = MENU_ADDRESS;
        }
        set_menu(temp);
    }
}
/* 工程师模式下PAGE按键处理函数 */
static void btn_process_page_engineer(void)
{
    panel_tubex_blink_set(2, 0);
    panel_unitx_blink_set(2, 0);
    set_select_state(0);
    set_page(PAGE_SYSTEM);
    set_menu(MENU_CALIB);
}
/* 高级模式下POWER按键处理函数 */
static void btn_process_power_advance(void)
{
    panel_tubex_blink_set(2, 0);
    panel_unitx_blink_set(2, 0);
    set_select_state(0);
    set_page(PAGE_SYSTEM);
    set_menu(MENU_VERSION);
}
/* 高级模式下MODE按键处理函数 */
static void btn_process_mode_advance(void)
{
    uint8_t temp = !get_selected_state();
    if(get_menu() == MENU_VERSION)
    {
        temp = 0;
    }
    set_select_state(temp);

    if(temp != 0)
    {
        panel_tubex_blink_set(2, 0xFFFF);
    }
    else
    {
        panel_tubex_blink_set(2, 0);
    }  
}
/* 高级模式下SPEED按键处理函数 */
static void btn_process_speed_advance(void)
{
    /* 高级模式下SPEED无操作 */
}
/* 高级模式下UP按键处理函数 */
static void btn_process_up_advance(void)
{
    uint8_t temp = get_selected_state();

    if(temp != 0)
    {
        panel_tubex_blink_set(2, 0xFFFF);
        switch(get_menu())
        {
            case MENU_VERSION:
            {

            }
            break;
            case MENU_BAUDRATE:
            {
                temp = get_baudrate();
                if(temp < BAUDRATE_9600)
                {
                    temp++;
                    set_baudrate(temp);
                }
            }
            break;
            case MENU_MACHINE_ENABLE:
            {
                
            }
            break;
            case MENU_LUMINANCE:
            {
                temp = get_panel_luminance();
                if(temp < 8)
                {
                    temp++;
                    set_panel_luminance(temp);
                }
            }
            break;
            case MENU_RELAY_MODE:
            {
                temp = get_relay_mode();
                if(temp == RELAY_MODE_DN_FRESH)
                {
                    temp = RELAY_MODE_AC;
                    set_relay_mode(temp);
                }
            }
            break;
        }
    }
    else
    {
        temp = get_menu();
        if(temp < MENU_RELAY_MODE)
        {
            temp++;
        }
        else
        {
            temp = MENU_VERSION;
        }
        set_menu(temp);
    }
}
/* 高级模式下DOWN按键处理函数 */
static void btn_process_down_advance(void)
{
    uint8_t temp = get_selected_state();

    if(temp != 0)
    {
        panel_tubex_blink_set(2, 0xFFFF);
        switch(get_menu())
        {
            case MENU_VERSION:
            {

            }
            break;
            case MENU_BAUDRATE:
            {
                temp = get_baudrate();
                if(temp > BAUDRATE_2400)
                {
                    temp--;
                    set_baudrate(temp);
                }
            }
            break;
            case MENU_MACHINE_ENABLE:
            {
                
            }
            break;
            case MENU_LUMINANCE:
            {
                temp = get_panel_luminance();
                if(temp > 1)
                {
                    temp--;
                    set_panel_luminance(temp);
                }
            }
            break;
            case MENU_RELAY_MODE:
            {
                temp = get_relay_mode();
                if(temp == RELAY_MODE_AC)
                {
                    temp = RELAY_MODE_DN_FRESH;
                    set_relay_mode(temp);
                } 
            }
            break;
        }
    }
    else
    {
        temp = get_menu();
        if(temp > MENU_VERSION)
        {
            temp--;
        }
        else
        {
            temp = MENU_RELAY_MODE;
        }
        set_menu(temp);
    }
}
/* 高级模式下PAGE按键处理函数 */
static void btn_process_page_advance(void)
{
    panel_tubex_blink_set(2, 0);
    panel_unitx_blink_set(2, 0);
    set_select_state(0);
    set_page(PAGE_SYSTEM);
    set_menu(MENU_VERSION);
}
